<?php

class postgresql_session
{
 var $sdata;
 var $id;
 var $dblink;
 var $gctime = 1800;
 
 //Constructor
 //Must be called before any output is generated 
 function postgresql_session ($dblink, $sessionid='') {
   $this->id = $sessionid;
   $this->sdata = array();
   $this->dblink = $dblink;
 }
 
 //Name   : start
 //Description: Starts a new session or loads previously stored values for a session
 //Returns    : -
 //Parameter  : -
 function start() {
  $this->gc();
  if(empty($this->id)) {
   //no session id found
   //generate a new one
   srand ((double) microtime() * 1000000);
   $this->id = md5(uniqid(rand()));
   $this->save();
  } else {
   //session id found
   $query = "SELECT val FROM session WHERE sid='".$this->id."';";
   $result = @pg_exec($this->dblink, $query);
   if(@pg_cmdtuples($result) == 0) {
     //no session id found
     //regenerate session
     $this->save();
    } else {
  	 $sessiondata = @pg_fetch_row($result, 0);
   	 $rest = unserialize($sessiondata[0]);
     foreach($rest as $varname=>$val) {
     $this->put($varname, $val); 	
    }
   }
  }
 }

 //Name   : register
 //Description: Registers a session variable
 //Returns    : -
 //Parameter  : variablename
 function put($varname, $var) {
  $this->sdata[$varname] = $var;
 }
 
 function get($varname) {
 	$this->updatetime();
 	return $this->sdata[$varname];
 }
 
 function getsessionid() {
 	$this->updatetime();
 	return $this->id;
 }

 //Name   : unregister
 //Description: Unregisters a session variable
 //Returns    : -
 //Parameter  : variablename
 function del($varname) {
  unset($this->sdata[$varname]);
  $this->save();
 }

 //Name   : destroy
 //Description: Stops a current session
 //Returns    : -
 //Parameter  : -
 function destroy() {
  $query = "DELETE FROM session WHERE sid ='".$this->id."';";
  @pg_exec($this->dblink, $query);
  $this->sdata = array();
  $this->id = '';
  return true;
 }

 //Name   : isregistered
 //Description: Checks if a variable is registered in a session
 //Returns    : 1(true) or 0(false)
 //Parameter  : variablename
 function exists($name) {
  $this->updatetime();
  return isset($this->sdata[$name]);
 }

 //Name   : save
 //Description: Saves the session array
 //Returns    : -
 //Parameter  : -
 function save () {
  $ts = serialize($this->sdata);
  $sqldate = date("YmdHis", time());
  $query = "UPDATE session SET val='".$ts."', changed=".$sqldate." WHERE sid='".$this->id."';";
  $result = @pg_query($this->dblink, $query);
  if(@pg_cmdtuples($result) == 0) {
   $query = "INSERT INTO session (sid, val, changed) VALUES ('".$this->id."', '".$ts."', ".$sqldate.");";
   $result = @pg_query($this->dblink, $query);
    }
 }

 function updatetime () {
  $sqldate = date("YmdHis", time());
  $query = "UPDATE session SET changed=".$sqldate." WHERE sid='".$this->id."';";
  $result = @pg_query($this->dblink, $query);
 }
 
 //Name   : gc
 //Description: Deletes sessions older than the gctime value from the database
 //Returns    : -
 //Parameter  : Time in seconds
  function gc() {
  $sqldate = date("YmdHis", time() - $this->gctime);
  $query = "DELETE FROM session WHERE changed < '$sqldate';";
  $result = @pg_exec($this->dblink, $query);
 }

}
